/**
 * CusContactsServiceImpl.java
 * Created at 2017-3-2
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.basic.model.TmEmploy;
import com.broadtext.rms.basic.service.ICusContactsService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: CusContactsServiceImpl</p>
 * <p>Description: 客户联系人服务层</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-3-2</p>
 */
@Service("cusContactsServiceImpl")
public class CusContactsServiceImpl extends EntityServiceImpl<TmEmploy> implements ICusContactsService {

    /**
     * <p>
     * Description: 注解入相关DAO
     * </p>
     * 
     * @param entityDao
     */
    @Autowired
    @Qualifier("tmEmployDao")
    public void setEntityDao(EntityDao<TmEmploy> entityDao) {
        super.entityDao = entityDao;
    }
    
    /**
     * 
     * <p>Description: 查询客户联系人列表</p>
     * @param map 查询条件
     * @param pageBounds 翻页信息
     * @return 联系人
     * @throws ServiceException 异常
     */
    @SuppressWarnings("rawtypes")
    @Override
    public EntityPageBean getCusContacts(Map<String, Object> map, PageBounds pageBounds) throws ServiceException {
        EntityPageBean pb;
        pb = new EntityPageBean();
        try {
            pb = this.entityDao.findEntityPageByProperty("com.broadtext.rms.basic.model.TmEmploy.findContacts",
                    map, pageBounds);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return pb;
    }

    /**
     * 
     * <p>Description: (批量伪)删除联系人</p>
     * @param idArray 联系人ID
     * @param sysUser 当前操作人
     * @throws ServiceException 异常
     */
    @Override
    public void deleteContactsByIds(String[] idArray, String sysUser) throws ServiceException {
        for (String id : idArray) {
            TmEmploy emp;
            emp = new TmEmploy();
            emp.setPkId(id);
            emp.setUpdateUser(sysUser);
            emp.setUpdateDate(new Date());
            try {
                this.entityDao.updateRecord("com.broadtext.rms.basic.model.TmEmploy.deleteContacts", emp);
            } catch (DaoException e) {
                throw new ServiceException(e);
            }
        }
    }

    /**
     * 
     * <p>Description: 根据选择的客户查询厂区</p>
     * @param customId 客户ID
     * @return 厂区
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> findFactoryByCustomer(String customId) throws ServiceException {
        List<Object> list;
        list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustomFactory.findFactoryOption", customId);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return list;
    }

    /**
     * 
     * <p>Description: 客户联系人查询客户</p>
     * @return 客户
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> findCustomerList(String parentId) throws ServiceException {
        List<Object> list;
        list = new ArrayList<Object>();
        List<Map<String,Object>> cusIdList;
        cusIdList = new ArrayList<Map<String,Object>>();
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        List<String> ids = new ArrayList<String>() ;
        if(StringUtils.isNotBlank(parentId)){
            queryMap.put("rootId", parentId);
        }
        try {
            //查询本级和本下级客户id
            cusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findTreeCusIds", queryMap);
            for (Map<String,Object> map : cusIdList) {
                ids.add(map.get("PK_ID")+"");
            }
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findCustomOpt", ids);
            
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }

    /**
     * 
     * <p>Description: 验证操作删除人员是否已绑定用户</p>
     * @param idArray 人员ID
     * @return 验证结果:true-已绑定；false-未绑定
     * @throws ServiceException 异常
     */
    @SuppressWarnings("rawtypes")
    @Override
    public boolean checkUserBind(String[] idArray) throws ServiceException {
        List user;
        for (String id : idArray) {
            try {
                user = this.entityDao.findByProperty("com.broadtext.rms.system.model.SysUser.findByUserId", id);
                if (user.size() != 0) {
                    return true;
                }
            } catch (DaoException e) {
                throw new ServiceException(e);
            }
        }
        return false;
    }

}
